package com.hualude.roadsign.repository;

import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.OrgDto;
import com.hualude.roadsign.model.OrgFatherAndChildEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by xu-jiaHui on 2017/4/9.
 */
public interface OrgFatherAndChildRepository extends CrudRepository<OrgFatherAndChildEntity, Long> {

    /**
     * @Description: 删除orgId和mainOrgId之间的联系
     * @Author: xu-jiaHui
     * @Date: 2017/4/9 22:27
     */
    @Modifying
    @Query(value = "delete from OrgFatherAndChildEntity where orgId=?1 and mainOrgId=?2")
    public Integer deleteOrgFatherAndChildByOrgId(Integer orgId, Integer mainOrgId) throws Exception;

    /**
     * @Description: 删除指定OrgId的所有监管单位
     * @Author: xu-jiaHui
     * @Date: 2017/4/12 21:21
     */

    @Modifying
    @Query(value = "delete from OrgFatherAndChildEntity where orgId=?1")
    public Integer deleteMainOrgByOrgId(Integer orgId) throws Exception;

    @Modifying
    @Query(value = "delete from OrgFatherAndChildEntity where mainOrgId=?1")
    public Integer deleteMainOrgByMOrgId(Integer orgId) throws Exception;

    /**
     * @Description: 删除和该公司有关的数据
     * @Author: xu-jiaHui
     * @Date: 2017/5/1 23:22
     */
    @Modifying
    @Query(value = "delete from OrgFatherAndChildEntity where orgId=?1 or mainOrgId=?1")
    public Integer deleteAllByOrgId(Integer orgId) throws Exception;

    /**
     * @Description: 获取指定orgId的监管单位用于DT
     * @Author: xu-jiaHui
     * @Date: 2017/4/12 17:58
     */
    public List<OrgDto> getMainOrgIdAndNameByOrgId(DTRequestDto dtRequestDto, Integer orgId) throws Exception;


    public Integer getMainOrgCountByOrgId(DTRequestDto dtRequestDto, Integer orgId) throws Exception;

    /**
     * @Description: 获取指定OrgId的监管单位
     * @Author: xu-jiaHui
     * @Date: 2017/4/27 13:56
     */
    @Query(value = "select o.orgName from OrgEntity as o,OrgFatherAndChildEntity as a where a.orgId=?1 and o.orgId=a.mainOrgId")
    public List<String> getMainOrgNameByOrgId(Integer orgId) throws Exception;


    /**
     * @Description: 获取指定orgId的养护单位
     * @Author: xu-jiaHui
     * @Date: 2017/4/27 14:05
     */
    @Query(value = "select o.orgName from OrgEntity as o,OrgFatherAndChildEntity as a where a.mainOrgId=?1 and o.orgId=a.orgId")
    public List<String> getOrgIdNameByOrgId(Integer orgId) throws Exception;

}
